package org.framework.lazy.cloud.network.heartbeat.client.netty.advanced;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClientPermeateServerVisitorSocket;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.client.AbstractHandleDistributeClientPermeateServerInitTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.MessageTypeEnums;
import org.wu.framework.inner.lazy.orm.spring.SpringConnectionHandler;
import org.wu.framework.spring.utils.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 客户端渗透服务端init信息
 *
 * @see MessageTypeEnums#DISTRIBUTE_CLIENT_PERMEATE_SERVER_INIT
 */
@Slf4j
public class ClientHandleDistributeClientPermeateServerInitTypeAdvanced extends AbstractHandleDistributeClientPermeateServerInitTypeAdvanced<NettyProxyMsg> {

    private final NettyClientProperties nettyClientProperties;

    public ClientHandleDistributeClientPermeateServerInitTypeAdvanced(NettyClientProperties nettyClientProperties) {
        this.nettyClientProperties = nettyClientProperties;
    }

    /**
     * 处理当前数据
     *
     * @param channel       当前通道
     * @param nettyProxyMsg 通道数据
     */
    @Override
    public void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
        // 初始化 客户端渗透服务端socket
        byte[] clientIdBytes = nettyProxyMsg.getClientId();
        byte[] visitorPort = nettyProxyMsg.getVisitorPort();
        byte[] clientTargetIp = nettyProxyMsg.getClientTargetIp();
        byte[] clientTargetPort = nettyProxyMsg.getClientTargetPort();

        List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
        NettyClientPermeateServerVisitorSocket nettyClientPermeateServerVisitorSocket = NettyClientPermeateServerVisitorSocket.NettyVisitorSocketBuilder.builder()
                .builderClientId(new String(clientIdBytes))
                .builderClientTargetIp(new String(clientTargetIp))
                .builderClientTargetPort(Integer.parseInt(new String(clientTargetPort)))
                .builderVisitorPort(Integer.parseInt(new String(visitorPort)))
                .builderNettyClientProperties(nettyClientProperties)
                .builderHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList)
                .build();
        try {
            nettyClientPermeateServerVisitorSocket.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
